/*
 * sfs_func.c
 *
 *  Created on: Mar 21, 2010
 *      Author: Li Liu
 */

#include "blockio.h"
#include "sfstest.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/* size of blocks on simulated disk */
#define BLKSIZE  128
/* number of blocks on simulated disk */
#define NUMBLKS  512

/*This is where the root block is located*/
#define ROOT_LK_INDEX  2

/*This is the number of files that can be open at one time*/
#define MAX_OPEN_FILES	4

/*In this design*/
#define NUM_CONTENT_IN_FOLDER  14

static super_blcok SB;
//Initializing the super_block
static int open_file_list[MAX_OPEN_FILES];

static int number_file_open;


int sfs_initialize(int erase){
    int i;
    floder_blcok root_blk;
    printf("Initializing the file system with the option 0\n");
	number_file_open=0;
    switch(erase) {
        case 0:
            printf("Initializing the file system with the option 0\n");
            printf("Trying to load the file structure.\n");
            get_block(0, SB.super_block_0);
            get_block(0, SB.super_block_1);            
            //printf("1st byte %x\n", SB.super_block_0[0]);

            return 1;
        case 1:
            printf("Initializing the file system with the option 1\n");
            printf("Trying to recreate the file system.\n");
            for (i=0;i<BLKSIZE;i++){
                if (i==0){
                    SB.super_block_0[i]=1;
                    printf("1st byte %x\n", SB.super_block_0[0]);
                }
                else SB.super_block_0[i]=0;
                SB.super_block_1[i]=0;
            }
            root_blk.type=0;
            root_blk.valid=1;
            strcpy (&(root_blk.name_list[0][0]),"test");
            for (i=0;i<NUM_CONTENT_IN_FOLDER;i++){
                root_blk.dest_list[i]=0;
            }

            
            root_blk.dest_list[0]=ROOT_LK_INDEX+1;

            put_block(0,SB.super_block_0);
            put_block(1,SB.super_block_1);


            put_block(ROOT_LK_INDEX,(char*)&root_blk);
            
            sfs_list_next_level(&root_blk);
            return 1;
   }

}



int sfs_open(char *pathname){
    floder_blcok * temp_f_blk;
    data_blcok * temp_d_blk;
    char *path_name_ptr=pathname;
    char file_name_buf[7];
    int buf_index=0;
    int block_index;
	//printf("The input Path name is :%s\n", pathname);
	//printf("First Char is: %d\n", *path_name_ptr);
    if(*path_name_ptr!='/'){
        printf("The path name should start with / .\n");
        return -1;
    }
    block_index=ROOT_LK_INDEX;
    //path_name_ptr++;
    while (*path_name_ptr!='\0'){
        path_name_ptr++;
        if (buf_index<7){
            if (*path_name_ptr=='/'){
				if (*(path_name_ptr+1)=='\0'){
					printf("You need to specify a file name not a folder name at the end\n");
					return -1;
				}
                //Just found a directory
                file_name_buf[buf_index]='\0';
				//printf("Directory Name is: %s\n",file_name_buf);
                get_block(block_index, (char*)temp_f_blk);
                block_index=sfs_find_next_level((floder_blcok*)block_index, file_name_buf);
                if (block_index>0){
                    printf("Going into next level of directory and the block number to the next level is %d\n",block_index);
                }else{
                    printf("Invalid path name!!\n");
                    return -1;
                }
				buf_index=0;
            }else if(*path_name_ptr=='\0'){
            	file_name_buf[buf_index]='\0';
                //printf("File Name is: %s\n",file_name_buf);
                get_block(block_index, (char*)temp_d_blk);
                block_index=sfs_find_next_level((data_blcok*)block_index, file_name_buf);
				if(block_index>0){
					printf("The spcified file exists");
					if (number_file_open==MAX_OPEN_FILES){
						printf("Can not open more files on the system\n")
						return -1;
					}else{
						//His the file and the file can be open, return the discripter
						number_file_open++;
						open_file_list[number_file_open]=block_index;
						return number_file_open;
					}
				}else{
					printf("Sorry, the file you specified does not exist!");
				}
                buf_index=0;
            }else{
                file_name_buf[buf_index]=*path_name_ptr;
            	buf_index++;
            }
        }else{
            buf_index=0;
            printf("File names in the path should not be more than 6 chars!.\n");
            return -1;
        }
    }
};

int
sfs_read(int fd, int start, int length, char *mem_pointer){

};

int sfs_write(int fd, int start, int length, char *mem_pointer){

};

int sfs_readdir(int fd, char *mem_pointer){

};

int sfs_close(int fd){

};

int sfs_delete(char *pathname){

};

int sfs_create(char *pathname, int type){

};

int sfs_getsize(char *pathname){

};

int sfs_gettype(char *pathname){
    
};


int sfs_find_next_level(floder_blcok* fb, char* filename){
    int i;
    int file_dest_ptr=0;
    for (i=0; i<NUM_CONTENT_IN_FOLDER; i++){
        if(strcmp (fb->name_list[i],filename) == 1){
            file_dest_ptr=fb->dest_list[i];
            break;
        }
    }

    return file_dest_ptr;

};

int sfs_list_next_level(floder_blcok* fb){
    int i;
    int file_dest_ptr=0;
    for (i=0; i<NUM_CONTENT_IN_FOLDER; i++){
        if(fb->dest_list[i]>ROOT_LK_INDEX){
            printf("%s ", fb->name_list[i]);
        }
    }
    printf("\n");
    return file_dest_ptr;

};
